


function [pointidx,distanceAlongVasc] = selectWidefieldVesselTrackingPoints(avios,traj)
% displays an image of the window and allows you to select a vessel
% interactively. 
% 
% INPUTS 
% avios: 2d image of the window that the tracking is registered to
% traj: the trajectories structure computed by highResWidefieldWrapper 
%
% OUTPUTS
% pointidx: the indices of the trajectories along the selected vessel 
% distanceAlongVasc - the distance (in pixels) along the selection from the
% first vertex

% display the window and the vessel positions:
figure
imagesc(avios);colormap('gray');axis equal;
hold on
if isstruct(traj)
    vp = traj(1).vesselCent;
elseif isfloat(traj)
    vp = traj;
end
plot(vp(:,1),vp(:,2),'r.');

% draw a polyline along that vasculature
selectionLine = drawpolyline;

startpoint = false(size(avios));startpoint(round(selectionLine.Position(1,2)),round(selectionLine.Position(1,1))) = true;
SE = strel("disk",15);
BW = createMask(selectionLine);
BW = imdilate(BW,SE);
D = bwdistgeodesic(BW,startpoint,'quasi-euclidean');
temp = round(vp);
temp = sub2ind(size(avios),temp(:,2),temp(:,1));
temp = D(temp);

[distanceAlongVasc,pointidx] = sort(temp,'ascend');
pointidx = pointidx(~isnan(distanceAlongVasc));
distanceAlongVasc = distanceAlongVasc(~isnan(distanceAlongVasc));

plot(vp(pointidx,1),vp(pointidx,2),'r.-')